package com.javatian.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javatian.dao.CartInfoDao;
import com.javatian.entity.Account;
import com.javatian.entity.CartInfo;
import com.javatian.entity.GoodsInfo;
import com.javatian.service.*;
import exception.CustomException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 购物车信息对外提供的服务的实现类
 */
@Service("cartInfoService")
public class CartInfoServiceImpl implements CartInfoService {

    @Resource
    private CartInfoDao cartInfoDao;

    @Resource
    private TypeInfoService typeInfoService;

    @Resource
    private AdminInfoService adminInfoService;

    @Resource
    private WorkerInfoService workerInfoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private GoodsInfoService goodsInfoService;

    /**
     * 增加一条记录
     *
     */
    @Override
    public CartInfo add(CartInfo cartInfo) {
        //先查询购物车里有没有这个用户的这个商品，有就更新数量，没有就添加
        Long userId = cartInfo.getUserId();
        Long goodsId = cartInfo.getGoodsId();
        Example example = new Example(CartInfo.class);
        example.createCriteria().andEqualTo("userId",userId).andEqualTo("level",cartInfo.getLevel())
                .andEqualTo("goodsId",goodsId);
        List<CartInfo> infos = cartInfoDao.selectByExample(example);
        if (CollectionUtil.isEmpty(infos)){
            //新增
            cartInfo.setCreateTime(DateUtil.formatDateTime(new Date()));
            cartInfoDao.insertSelective(cartInfo);
            return cartInfo;
        }else {
            //更新
            CartInfo info = infos.get(0);
            info.setCount(cartInfo.getCount() + info.getCount());
            cartInfoDao.updateByPrimaryKeySelective(info);
            return info;
        }
    }

    /**
     * 删除一条记录
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        cartInfoDao.deleteByPrimaryKey(id);
    }

    /**
     * 修改一条记录
     * @param cartInfo
     */
    @Override
    public void update(CartInfo cartInfo) {
            cartInfoDao.updateByPrimaryKeySelective(cartInfo);
    }

    /**
     * 根据主键查询一条记录
     *
     * @param id
     */
    @Override
    public CartInfo findById(Long id) {
        return cartInfoDao.selectByPrimaryKey(id);
    }

    /**
     * 查询某个用户的所有购物车里面的所有商品
     */
    @Override
    public List<GoodsInfo> findAll(Long userId, Integer level) {
        List<GoodsInfo> goodsList = new ArrayList<>();
        List<CartInfo> cartInfoList = cartInfoDao.findCartByUserId(userId, level);
        for (CartInfo cartInfo:cartInfoList) {
            GoodsInfo goods = goodsInfoService.findById(cartInfo.getGoodsId());
            if (goods!=null){
                //注意这里返回的count是用户加入商品的数量，并不是商品的库存
                goods.setCount(cartInfo.getCount());
                goodsList.add(goods);
            }

        }
        return goodsList;
    }

    /**
     * 根据条件分页查询
     * 管理员可以看所有的购物车列表，其他用户只能看自己的购物车列表
     * @param pageNum  第几页
     * @param pageSize 每页数量
     * @param user 用户信息
     * @return 当前页的List列表
     */
    @Override
    public PageInfo<CartInfo> findPage(Integer pageNum, Integer pageSize, Account user) {
        PageHelper.startPage(pageNum,pageSize);
        if (user==null){
            throw new CustomException("1001","用户未登录");
        }
        Integer userLevel = user.getLevel();

        List<CartInfo> infoList = null;
        if (userLevel == 1){
            infoList = cartInfoDao.findAll();
            //处理一下userName
            for (CartInfo info:infoList) {
                Long userId = info.getUserId();
                Integer level = info.getLevel();
                if (level == 1){
                    info.setUserName(adminInfoService.findById(userId).getName());
                }else if (level == 2){
                    info.setUserName(workerInfoService.findById(userId).getName());
                }else if (level == 3){
                    info.setUserName(userInfoService.findById(userId).getName());
                }

            }

        }else {
           infoList = cartInfoDao.findCartByUserId(user.getId(),user.getLevel());
        }


        return PageInfo.of(infoList);
    }

    /**
     * 清空购物车
     */
    @Override
    public void empty(Long userId, Integer level) {
        cartInfoDao.deleteByUserId(userId,level);
    }

    /**
     * 删除某用户购物车里的某个商品
     */
    @Override
    public void deleteGoods(Long userId, Integer level, Long goodsId) {
        cartInfoDao.deleteGoods(userId,level,goodsId);
    }


}




















